<?php
class Folder_management extends MY_Controller{
	
 	function __construct()
    {
    	parent::__construct();
        /*$this->load->library('session');*/
    }
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
	function index(){
		$this->_setLayout('admin/folder_management','admin_layout');
	}
	function add_folder(){
    	$callback = $_REQUEST['callback'];
    	$jsonOutPut = '';
		try{
	    	$data = json_decode ($_GET['data'],true);
	    	if(empty($data)){
	    		$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not load'));	    			    		
	    	}
	    	else{
		    	$this->load->model('Folder');
		    	if(isset($data['id'])){
		    		$this->Folder->update($data,$data['id']);
		    		$id = $data['id'];
		    	}	
		    	else{	
		    		$id = $this->Folder->insert($data);
		    		
		    	}	
		    	$data_return = $this->Folder->get_by_id($id);
		    	$data_return['pictures'] = '<a class="thickbox" href="#" onClick="thickBoxGallery('.$id.')">Select pictures</a>';
		    	$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$data_return));	 
	    	}
	    	   	
    	}
    	catch (Exception $e){
    		$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not load'));
    	}
    	if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}	
    }
	function delete_folder(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);			
    		$this->load->model('Folder');
    		$this->load->model('Folder_picture');
    		$current = $this->Folder->get_by_id($data);
    		$this->Folder_picture->delete_by_field(array('folder_id'=>$data));
			$this->Folder->delete($data);
			$jsonOutPut = json_encode(array('success'=>true));
		}
    	catch (Exception $e){
    		$jsonOutPut = json_encode(array('success'=>false));
    	}
		if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}
    }
	function folder_grid(){
    	$this->load->model('Folder');
	    $callback = $_REQUEST['callback'];
	    $data = array();
	    $i = 0;
	    foreach($this->Folder->get_list($_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'id',(isset($_GET['dir']))?$_GET['dir']:'ASC') as $row){
	    	$data[] = array_merge($row,array('pictures'=>'<a class="thickbox" href="#" onClick="thickBoxGallery('.$row['id'].')">Select pictures</a>'));
	    }
		// Create the output object.
		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','start'=>$_GET['start'],'total'=>$this->Folder->get_total(),'data'=>$data));
		//start output
		if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}
    }
	
}